<template>
	<view>
		<view>
			<uni-section title="修改" type="line">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-row class="demo-uni-row">
						<uni-col :span="12">
							<uni-forms-item label-width="100px" label="泡泡号" class="input">
								<uni-easyinput disabled v-model="baseFormData.chitName" placeholder="" />
							</uni-forms-item>
						</uni-col>
						<uni-col :span="12">
							<uni-forms-item label-width="100px" label="测试项目" class="input">
								<uni-easyinput disabled v-model="baseFormData.characteristics" placeholder="" />
							</uni-forms-item>
						</uni-col>
					</uni-row>
					<uni-row class="demo-uni-row" v-show="type==false">
						<uni-col :span="12">
							<uni-forms-item label-width="100px" required="" label="判断型:" class="input">
								<uni-data-select v-model="value" :localdata="range"></uni-data-select>
							</uni-forms-item>
						</uni-col>
					</uni-row>
					<uni-row class="demo-uni-row" v-show="type">
						<uni-col :span="12">
							<uni-forms-item label-width="100px" label="最大值:" class="input">
								<uni-easyinput disabled v-model="baseFormData.max" placeholder="" />
							</uni-forms-item>
						</uni-col>
						<uni-col :span="12">
							<uni-forms-item label-width="100px" label="最小值:" class="input">
								<uni-easyinput disabled v-model="baseFormData.min" placeholder="" />
							</uni-forms-item>
						</uni-col>
					</uni-row>
					<uni-row class="demo-uni-row" v-show="type">
						<uni-col :span="12">
							<uni-forms-item label-width="100px" label="测试值:" class="input">
								<uni-easyinput v-model="baseFormData.inspectionValue" placeholder="" />
							</uni-forms-item>
						</uni-col>
						<!-- <uni-col :span="12">
							<uni-forms-item label-width="100px" label="超差值:" class="input">
								<uni-easyinput v-model="baseFormData.chaocha" placeholder="" />
							</uni-forms-item>
						</uni-col> -->
					</uni-row>
				</uni-forms>
			</uni-section>
		</view>
		<view v-show="show">
			<uni-section title="点击拍照" type="line">
				<view class="uni-file-picker__container" style="margin-left: 2%;">
					<view class="file-picker__box" v-for="(item,index) in imageList" :key="index">
						<view class="file-picker__box-content">
							<image class="file-image" :src="item" mode="aspectFill"
								@click.stop="prviewImage(item,index)">
							</image>
							<view class="icon-del-box" @click.stop="delFile(index)">
								<view class="icon-del"></view>
								<view class="icon-del rotate"></view>
							</view>
						</view>
					</view>
					<view class="file-picker__box">
						<view class="file-picker__box-content is-add" @click="choose">
							<slot>
								<view class="icon-add"></view>
								<view class="icon-add rotate"></view>
							</slot>
						</view>
					</view>
				</view>
			</uni-section>
			<uni-section title="异常描述" type="line">
				<textarea class="textarea-a" v-model="text" rows="5" maxlength="100000"></textarea>
				<view style="height: 70px;margin-top: 1%;padding-bottom: 2%;">
					<button class="mini-btn " style="width: 50%; margin-left: 25%;" @click="Submit"
						type="primary">保存</button>
				</view>
			</uni-section>
		</view>
		<uni-popup ref="popup" background-color="#fff">
			<view style="width:35vh;height:15vh;/">
				<button type="primary" style="width:15vh; margin-top:2vh;" @click="photo">拍照</button>
				<button type="primary" style="width:15vh; margin-top:1vh;" @click="album">相册</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import config from '@/config'
	import {
		getToken
	} from '@/utils/auth'
	import {
		saveValue,
		save,
		file
	} from '@/api/system/quality.js'
	export default {
		data() {
			return {
				baseFormData: {
					chitName: "",
					characteristics: "",
					max: "",
					min: "",
					inspectionValue: "",
					chaocha: "",
				},
				range: [{
						value: "NG",
						text: "NG"
					},
					{
						value: "PASS",
						text: "PASS"
					},
				],
				value: "",
				workItem: {},
				imageList: [],
				filePath: [],
				show: true,
				type: false,
				text: "",
			}
		},
		onLoad(params) {
			this.workItem = JSON.parse(params.parme)
			console.log(this.workItem)
			if (this.workItem.chitValuetype == "0") {
				this.type = true;
				// if (this.workItem.max < this.workItem.inspectionValue) {
				// 	this.baseFormData.chaocha = this.floatSub(this.workItem.inspectionValue, this.workItem.max)
				// }
				// if (this.workItem.min > this.workItem.inspectionValue) {
				// 	this.baseFormData.chaocha = this.floatSub(this.workItem.min, this.workItem.inspectionValue)
				// }
			} else {
				this.value = this.workItem.inspectionValue;

			}
			this.baseFormData = JSON.parse(params.parme)

		},
		methods: {
			// inputchanghe(){
			// 	if (this.baseFormData.max < this.baseFormData.inspectionValue) {
			// 		this.baseFormData.chaocha = this.floatSub(this.baseFormData.inspectionValue, this.baseFormData.max)
			// 	}
			// 	if (this.baseFormData.min > this.baseFormData.inspectionValue) {
			// 		this.baseFormData.chaocha = this.floatSub(this.baseFormData.min, this.baseFormData.inspectionValue)
			// 	}
			// },
			// floatSub(arg2, arg1) {
			//   var r1, r2, m, n;
			//   try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
			//   try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
			//   m = Math.pow(10, Math.max(r1, r2));
			//   //lastmodifybydeeka  
			//   //动态控制精度长度  
			//   n = (r1 >= r2) ? r1 : r2;
			//   return ((arg2 * m - arg1 * m) / m).toFixed(n);
			// },
			choose() {
				this.$refs.popup.open();
			},
			photo() {
				this.$refs.popup.close();
				var camera = plus.camera.getCamera() // 获取camera对象
				var resolution = camera.supportedImageResolutions // 获取字符串数组，摄像头支持的摄像分辨率
				camera.captureImage((res) => {
					console.log(res)
					this.imageList.push(res)
					console.log(this.imageList)
				}, (error) => {
					console.log(error)
				}, {
					resolution: resolution[resolution.length - 1],
					format: "jpg"
				})
			},
			album() {
				var that = this;
				this.$refs.popup.close();
				uni.chooseImage({
					count: 1000, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths))
						for (var i = 0; i < res.tempFilePaths.length; i++) {
							that.imageList.push(res.tempFilePaths[i]);
						}
					}
				});
			},
			// picturezip() {
			// 	this.$modal.loading("保存中，请耐心等待...")
			// 	for (var i = 0; i < this.imageList.length; i++) {
			// 		this.upload(this.imageList[i])
			// 	}
			// },
			upload() {
				// const baseUrl = config.baseUrl;
				// uni.uploadFile({
				// 	url: baseUrl + '/development/v9/SceneAbnormalFeedback/upload',
				// 	filePath: tempFilePaths,
				// 	name: 'file',
				// 	method: 'POST',
				// 	header: {
				// 		'Authorization': getToken()
				// 	},
				// 	success: (uploadFileRes) => {
				// 		//console.log(uploadFileRes.data)
				// 		var file = {
				// 			fileUrl: uploadFileRes.data
				// 		}
				// 		this.filePath.push(file)
				// 		console.log(this.filePath)
				// 	}
				// });
				// if (this.imageList.length === 0) {
				// 	this.$modal.msgError("上传文件为空！");
				// 	return
				// }
				this.$modal.loading("保存中，请耐心等待...")
				const baseUrl = config.baseUrl;
				let files = [];
				let that = this;
				for (var i = 0; i < this.imageList.length; i++) {
					const path = this.imageList[i];
					const splitArray = path.split("/");
					const substring = splitArray[splitArray.length - 1];
					files.push({
						"name": substring,
						"uri": this.imageList[i],
					})
				}
				//console.log(files)
				const uploadTask = uni.uploadFile({
					url: baseUrl + '/development/v9/bcBenchJhController/uploadFileList', //仅为示例，非真实的接口地址
					files: files,
					//name: 'file',
					formData: {
						dataId: this.workItem.tableVauleid,
						appName: "沈阳富创平板"
					},
					header: {
						'Authorization': getToken()
					},
					success: (uploadFileRes) => {
						console.log();
						if (JSON.parse(uploadFileRes.data).code == 200) {
							this.TASK();
						} else {
							this.$modal.closeLoading();
							this.$modal.msgError("保存文件失败");
						}
					},
					fail: (uploadFileRes) => {
						this.$modal.closeLoading();
						this.$modal.msgError("保存失败");
					},
				});
				uploadTask.onProgressUpdate((res) => {
					//console.log('上传进度' + res.progress);
					//console.log('已经上传的数据长度' + res.totalBytesSent);
					//console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
				});

			},
			delFile(indexs) {
				var file = this.imageList;
				this.imageList = file.filter(function(value, index) {
					return index !== indexs; // 返回下标不为2的元素组成的新数组
				});
			},
			prviewImage(img, index) {
				let urls = []
				if (this.disablePreview) return
				this.imageList.forEach(i => {
					urls.push(i)
				})
				uni.previewImage({
					urls: urls,
					current: index
				});
			},
			// 保存
			Submit() {
				var that = this;
				var inspectionValue = null;
				if (this.workItem.chitValuetype == "0") {
					inspectionValue = this.baseFormData.inspectionValue
				} else {
					inspectionValue = this.value
				}
				console.log(inspectionValue)
				if (inspectionValue === null) {
					this.$modal.msgError("实测值不能为空")
				} else {
					//this.picturezip();
					that.upload()
					// var intervalId = setInterval(function() {
					// 	if (that.filePath.length == that.imageList.length) {
					// 		that.TASK();
					// 		clearInterval(intervalId);
					// 	}
					// }, 1000);
				}
			},
			TASK() {
				var id = this.workItem.tableVauleid
				var inspectionValue = ""
				if (this.workItem.chitValuetype == "0") {
					inspectionValue = this.baseFormData.inspectionValue
				} else {
					inspectionValue = this.value
				}
				var remark = this.text
				var data = {
					dataId: this.workItem.tableVauleid,
					fileUrl: JSON.stringify(this.filePath),
					appName: "沈阳富创平板"
				}
				this.filePath = [];
				saveValue(id, inspectionValue, remark).then(res => {
					//console.log(res)
					//if (this.value == "NG" || this.workItem.max < this.baseFormData.inspectionValue || this
					//	.workItem.min > this.baseFormData.inspectionValue) {
					//save(id, remark).then(res => {
					// file(data).then(res => {
					// 	this.$modal.msgSuccess("保存成功");
					// 	setTimeout(function() {
					// 	    uni.navigateBack({
					// 	        delta: 1 // 返回的页面数，这里设置为1表示返回上一页
					// 	    });
					// 	}, 1000);
					// 	this.$modal.closeLoading();
					// }).catch(error => {
					// 	reject(error)
					// 	this.$modal.closeLoading();
					// })
					// }).catch(error => {
					// 	reject(error)
					// 	this.$modal.closeLoading();
					// })
					//} else {
					this.$modal.closeLoading();
					this.$modal.msgSuccess("保存成功");
					setTimeout(function() {
						uni.navigateBack({
							delta: 1 // 返回的页面数，这里设置为1表示返回上一页
						});
					}, 1000);
					//}
				}).catch(error => {
					reject(error)
					this.$modal.closeLoading();
				})
			}
		}
	}
</script>

<style>
	.input {
		width: 90%;
		margin-left: 5%;
	}

	.uni-file-picker {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		overflow: hidden;
		width: 100%;
		/* #endif */
		flex: 1;
	}

	.uni-file-picker__container {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-wrap: wrap;
		margin: -5px;

	}

	.file-picker__box {
		background-color: #fff;
		position: relative;
		/* 	// flex: 0 0 33.3%; */
		width: 15%;
		padding-top: 15%;
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		/* #endif */
	}

	.uni-file-picker__container {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-wrap: wrap;
		margin: -5px;
	}

	.file-picker__box-content {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		margin: 5px;
		border: 1px #eee solid;
		border-radius: 5px;
		overflow: hidden;
	}

	.file-picker__progress {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		/* border: 1px red solid; */
		z-index: 2;
	}

	.file-picker__progress-item {
		width: 100%;
	}

	.file-picker__mask {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		position: absolute;
		right: 0;
		top: 0;
		bottom: 0;
		left: 0;
		color: #fff;
		font-size: 12px;
		background-color: rgba(0, 0, 0, 0.4);
	}

	.file-image {
		width: 100%;
		height: 100%;
	}

	.is-add {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		justify-content: center;
	}

	.icon-add {
		width: 50px;
		height: 5px;
		background-color: #f1f1f1;
		border-radius: 2px;
	}

	.rotate {
		position: absolute;
		transform: rotate(90deg);
	}

	.icon-del-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 3px;
		right: 3px;
		height: 26px;
		width: 26px;
		border-radius: 50%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 2;
		transform: rotate(-45deg);
	}

	.textarea-a {
		border: 1px #eee solid;
		width: 90%;
		height: 120px;
		margin-left: 5%;
	}

	.icon-del {
		width: 15px;
		height: 2px;
		background-color: #fff;
		border-radius: 2px;
	}

	::v-deep .is-disabled {
		color: black !important;
	}
</style>